﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmLeaveWorkList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _LWID = 0;
        int _department_ID, _EmployeeID;
        int _branch_ID;
        int _month = DateTime.Now.Month;
        int _year = DateTime.Now.Year;
        bool _loadedData = false;
        LeaveWorkClient _wcfLW;
        BranchClient wcfBranch;
        DepartmentClient wcfDepartment;
        RoleRightInfo listRole;
        MainServiceClient wcLWain;
        public frmLeaveWorkList()
        {
            InitializeComponent();
            LoadLanguage();
            wcfBranch = Service.Branch();
            wcfDepartment = Service.Department();
            _wcfLW = Service.LeaveWork();
            wcLWain = Service.MainService();
            listRole = UIParamater.Quyen_NgayNghiVaCongTac;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                this.btnEdit.Tag = "xem";
            }
            UISetting.LoadYears(ref cboYear);
            //cboYear.ComboBox.SelectedValue = Convert.ToUInt32(DateTime.Now.Year);

            UISetting.LoadTreeView3(ref tvBranch);
        }

        private void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        private void LoadData()
        {
            int temptYear;
            if (Int32.TryParse(cboYear.Text, out temptYear))
                _year = temptYear;

            string keyWord = "";
            if (txtKeyWord.Text.Trim().Contains("'"))
                keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                keyWord = txtKeyWord.Text.Trim();

            DataTable dt = _wcfLW.LeaveWork_GetList(out itemCount, currPage, pageSize, _year, _branch_ID, _department_ID, keyWord).Tables[0];
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();

            dgvLeaveWorkList.DataSource = dt;
            dgvLeaveWorkList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
            dgvLeaveWorkList.Columns["RowNumber"].Width = 50;
            dgvLeaveWorkList.Columns["RowNumber"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvLeaveWorkList.Columns["LW_ID"].Visible = false;
            dgvLeaveWorkList.Columns["LW_EmployeeID"].Visible = false;
            dgvLeaveWorkList.Columns["LW_BranchID"].Visible = false;
            dgvLeaveWorkList.Columns["LW_DepartmentID"].Visible = false;
            dgvLeaveWorkList.Columns["LW_LeaveID"].Visible = false;
            dgvLeaveWorkList.Columns["FullName"].HeaderText = UpdateUI.GetString("hovaten");
            dgvLeaveWorkList.Columns["FullName"].Width = 150;
            dgvLeaveWorkList.Columns["Leave_Name"].HeaderText = UpdateUI.GetString("hinhthucnghi");
            dgvLeaveWorkList.Columns["Leave_Name"].Width = 140;
            dgvLeaveWorkList.Columns["LW_FromDate"].HeaderText = UpdateUI.GetString("tungay");
            dgvLeaveWorkList.Columns["LW_FromDate"].DefaultCellStyle.Format = "dd/MM/yyyy";
            dgvLeaveWorkList.Columns["LW_FromDate"].Width = 100;
            dgvLeaveWorkList.Columns["LW_ToDate"].HeaderText = UpdateUI.GetString("denngay");
            dgvLeaveWorkList.Columns["LW_ToDate"].DefaultCellStyle.Format = "dd/MM/yyyy";
            dgvLeaveWorkList.Columns["LW_ToDate"].Width = 100;
            dgvLeaveWorkList.Columns["LW_Reason"].HeaderText = UpdateUI.GetString("lydo");
            dgvLeaveWorkList.Columns["LW_Reason"].Width = 300;
            _loadedData = true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmLeaveWorkInfo(0, 0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvLeaveWorkList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    _LWID = Convert.ToInt32(dgvLeaveWorkList.CurrentRow.Cells["LW_ID"].Value);
                    _EmployeeID = Convert.ToInt32(dgvLeaveWorkList.CurrentRow.Cells["LW_EmployeeID"].Value);
                    Form f = new frmLeaveWorkInfo(_LWID, _EmployeeID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvLeaveWorkList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _LWID = Convert.ToInt32(dgvLeaveWorkList.CurrentRow.Cells["LW_ID"].Value);
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfLW.LeaveWork_Delete(_LWID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvLeaveWorkList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void frmLeaveWorkList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void btnNewPopUp_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDelPopUp_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void dgvLeaveWorkList_MouseDown(object sender, MouseEventArgs e)
        {
            if (dgvLeaveWorkList.SelectedRows.Count > 0)
            {
                if (e.Button == MouseButtons.Right)
                {
                    mnuPopup.Enabled = true;
                    btnNewPopUp.Enabled = true;
                    btnEditPopUp.Enabled = true;
                    btnDelPopUp.Enabled = true;
                    mnuView.Enabled = true;
                }
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtKeyWord.Text = "";
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    _branch_ID = 0;
                    _department_ID = 0;
                }
                else
                    _branch_ID = Program.intBranchID;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                _branch_ID = Convert.ToInt32(e.Node.Tag);
                _department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                _department_ID = Convert.ToInt32(e.Node.Tag);
                _branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();

            }
        }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                currPage = 0;
                _year = Convert.ToInt32(cboYear.Text);
                LoadData();
            }
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void frmLeaveWorkList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            if (dgvLeaveWorkList.Columns.Count > 0)
            {
                dgvLeaveWorkList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
                dgvLeaveWorkList.Columns["FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvLeaveWorkList.Columns["Leave_Name"].HeaderText = UpdateUI.GetString("hinhthucnghi");
                dgvLeaveWorkList.Columns["LW_FromDate"].HeaderText = UpdateUI.GetString("tungay");
                dgvLeaveWorkList.Columns["LW_ToDate"].HeaderText = UpdateUI.GetString("denngay");
                dgvLeaveWorkList.Columns["LW_Reason"].HeaderText = UpdateUI.GetString("lydo");
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (dgvLeaveWorkList.SelectedRows.Count > 0)
            {
                int _EID = Convert.ToInt32(dgvLeaveWorkList.CurrentRow.Cells["LW_EmployeeID"].Value);
                frmViewLeaveWork f = new frmViewLeaveWork(_EID, _year);
                f.ShowDialog();
            }
        }

    }
}
